package cn.itsource.crm.service.impl;

import cn.itsource.crm.mapper.BaseMapper;
import cn.itsource.crm.query.BaseQuery;
import cn.itsource.crm.service.IBaseService;
import cn.itsource.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public T findOne(Serializable id) {
        return baseMapper.findOne(id);
    }

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Serializable id) {
        baseMapper.delete(id);
    }

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        //涉及到高级查询   where ...条件，需要传入参数
        Long total = baseMapper.queryTotal(baseQuery);
        //还涉及到分页
        List<T> rows = baseMapper.queryData(baseQuery);

        //需要传入参数：total和rows
        return new PageList<>(total,rows);
    }
}
